package com.cqs.learning;


import com.cqs.learning.util.TimeUtil;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {

    private int age = 3;


    //    @StopWatcher
    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        while (true) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
//                    System.out.println(Thread.currentThread());
//                    Thread.interrupted();
//                    System.out.println(Thread.currentThread().getState());
//                    try {
//                        Thread.currentThread().stop();
//                    }finally {
//                        System.out.println(Thread.currentThread().getState());
//                    }
                }
            });
            break;
        }

        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread());
            }
        });

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(TimeUtil.curTime() + "\t" + Thread.currentThread());
                    try {
                        Thread.sleep(2000);
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(TimeUtil.curTime() + "\t" + Thread.currentThread());
                    try {
                        Thread.sleep(1000);
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }
}
